Completed
Push — master ( 88037b...111ea3 )
by Ajeh
27s
created

Directives.defineConfirm   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 14
rs 9.4285
nop 3
1
// Directives
2
3
import {noop, clickNode, cloneObj} from './utilities'
4
import {CONFIRM_TYPES} from './constants'
5
6
7
let Directives = function (Vue) {
8
    Object.defineProperties(this, {
9
        Vue: {get: () => Vue},
10
        confirmDefinition: {
11
            get: this.defineConfirm
12
        }
13
    })
14
}
15
16
Directives.prototype.getConfirmMessage =  function(binding) {
17
    if (binding.value && binding.value.message) {
18
        return binding.value.message
19
    }
20
    return typeof binding.value === 'string' ? binding.value : null
21
}
22
23
Directives.prototype.getOptions =  function(binding) {
24
    let options = typeof binding.value === 'object' ? cloneObj(binding.value) : {}
25
26
    delete options['ok']
27
    delete options['cancel']
28
29
    if(binding.arg && CONFIRM_TYPES.hasOwnProperty(binding.arg.toUpperCase())){
30
        options.type = CONFIRM_TYPES[binding.arg.toUpperCase()]
31
    }
32
33
    return options
34
}
35
36
Directives.prototype.getThenCallback =  function(binding, el){
37
    if (binding.value && binding.value.ok) {
38
        return binding.value.ok
39
    } else {
40
        return () => {
41
            // Unbind to allow original event
42
            el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
43
            // Trigger original event
44
            clickNode(el)
45
            // Re-bind listener
46
            el.addEventListener('click', el.VuejsDialog.clickHandler, true)
47
        }
48
    }
49
}
50
51
Directives.prototype.getCatchCallback =  function(binding) {
52
    if (binding.value && binding.value.cancel) {
53
        return binding.value.cancel
54
    }
55
    return noop
56
}
57
58
59
60
Directives.prototype.defineConfirm = function () {
61
    const DirectiveDefinition = {}
62
63
    const clickHandler = (event, el, binding) => {
64
        event.preventDefault()
65
        event.stopImmediatePropagation()
66
67
        let options = this.getOptions(binding)
68
        let confirmMessage = this.getConfirmMessage(binding)
69
        let thenCallback = this.getThenCallback(binding, el)
70
        let catchCallback = this.getCatchCallback(binding)
71
72
        this.Vue.dialog
73
            .confirm(confirmMessage, options)
74
            .then(thenCallback)
75
            .catch(catchCallback)
76
    }
77
78
    DirectiveDefinition.bind = (el, binding) => {
79
        el.VuejsDialog = el.VuejsDialog || {}
80
81
        el.VuejsDialog.clickHandler = function (event) {
82
            clickHandler(event, el, binding)
83
        }
84
85
        el.addEventListener('click', el.VuejsDialog.clickHandler, true)
86
    }
87
88
    DirectiveDefinition.unbind = (el) => {
89
        el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
90
    }
91
92
    return DirectiveDefinition
93
}
94
95
export default Directives
96